<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item :label="$t('m.system.user.entity.realname')" style="margin-left:8px">
              <a-input :placeholder="$t('m.bpm.design.select_user_search_username_placeholder')" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 18px">{{ $t('m.common.button.query') }}</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="userKey"
      :pagination="ipagination"
      :columns="columns"
      :dataSource="dataSource"
      :loading="loading"
      :rowSelection="{selectedRowKeys: computedKeys, onChange: onSelectChange}"
      @change="handleTableChange">
    </a-table>

  </a-card>
</template>

<script>

  import { YcListMixin } from '@/mixins/YcListMixin'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'SelectUserTreeRight',
    mixins: [YcListMixin],
    props: {
      value: {
        type: Array,
        required: true
      },
      currentDeptId: {
        type: String,
        default: null
      }
    },
    data () {
      return {
        disableMixinCreated: true,
        columns: [
          {
            title: this.$t('m.system.org.entity.depName'),
            align: 'center',
            dataIndex: 'deptName'
          },
          {
            title: this.$t('m.system.user.entity.realname'),
            align: 'center',
            dataIndex: 'name'
          },
        ],
        url: {
          list: YchConstant.PREFIX_BPM + '/bpm-back/processSelectRest/getUserListByDeptIdByPage/v1',
        }
      }
    },
    watch: {
      currentDeptId: {
        immediate: true,
        handler (value) {
          if (value) {
            this.queryParam = {
              deptId: value
            }
            this.loadData()
          } else {
            this.queryParam = {
              deptId: null
            }
            this.dataSource = []
          }
        }
      },
    },
    computed: {
      computedKeys () {
        return this.value.map(item => item.id + '#' + item.deptId)
      }
    },
    methods: {
      onSelectChange (selectedRowKeys, selectedRows) {
        const oldKeys = this.computedKeys
        const data = this.value.filter(item => selectedRowKeys.indexOf(item.id + '#' + item.deptId) != -1)//原来剩下的
        const addIds = selectedRowKeys.filter(id => oldKeys.indexOf(id) == -1)//新增的
        selectedRows.forEach(item => {
          if (addIds.indexOf(item.userKey) != -1) {
            data.push({
              id: item.id,
              name: item.name,
              deptId: item.deptId,
              deptName: item.deptName
            })
          }
        })
        this.$emit('input', data)
      },
      searchReset () {
        this.queryParam = {
          deptId: this.queryParam.deptId
        }
        this.loadData(1)
      },
    }
  }
</script>
